using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using MinimalistFoodStreet.BackEnd.Infrastructure.Attributes;
using MinimalistFoodStreet.BackEnd.Infrastructure.Dtos.Request.Functions;
using MinimalistFoodStreet.BackEnd.Infrastructure.Dtos.Request.Tables;
using MinimalistFoodStreet.BackEnd.Server.IServer.Tables;
using System.Collections.Generic;

namespace MinimalistFoodStreet.BackEnd.Api.Api.Controllers.Tables
{
    public class OrdersController : BaseController 
	{
		private readonly IOrdersServer _iOrdersServer;

		public OrdersController (IOrdersServer iOrdersServer)
		{
			_iOrdersServer = iOrdersServer;
		}
		
		/// <summary>
		/// 添加(生成订单)
		/// </summary>
		/// <param name="dto"></param>
		/// <returns></returns>
		[HttpPost]
		[CheckLogin(new CheckLoginRoles[] {CheckLoginRoles.business })]
		public async Task<IActionResult> Add(RequestCreateOrdersDto dto)
		{
			return Send(await _iOrdersServer.AddAsync(dto,_business.BId));
		}

		/// <summary>
		/// 获取订单详情
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpGet]
		[CheckLogin(new CheckLoginRoles[] {CheckLoginRoles.administrators,CheckLoginRoles.suppliers,CheckLoginRoles.business })]
		public async Task<IActionResult> Detail(int id)
        {
			return Send(await _iOrdersServer.DetailAsync(id));
		}

        /// <summary>
        /// 管理员端分页
        /// </summary>
        /// <param name="page">当前是第几页</param>
        /// <param name="limit">一页有多少条数据</param>
        /// <param name="key">要查询的关键字</param>
        /// <returns></returns>
        [HttpGet]
		[CheckLogin(new CheckLoginRoles[] {CheckLoginRoles.administrators})]
		public async Task<IActionResult> AdminPagination(int page, int limit, string? key, [FromQuery(Name = "statuss")] List<int>? statuss)
		{
			return Send(await _iOrdersServer.PaginationAsync(page, limit, key, null, null, statuss));
		}

        /// <summary>
        /// 供应商端分页
        /// </summary>
        /// <param name="page">当前是第几页</param>
        /// <param name="limit">一页有多少条数据</param>
        /// <param name="key">要查询的关键字</param>
        /// <param name="statuss">根据多个状态过滤</param>
        /// <returns></returns>
        [HttpGet]
		[CheckLogin(new CheckLoginRoles[] {CheckLoginRoles.suppliers})]
		public async Task<IActionResult> SupplierPagination(int page,int limit,string? key, [FromQuery(Name = "statuss")] List<int>? statuss)
		{
			return Send(await _iOrdersServer.PaginationAsync(page,limit,key,null,_supplier.SId, statuss));
		}

        /// <summary>
        /// 商户端分页
        /// </summary>
        /// <param name="page">当前是第几页</param>
        /// <param name="limit">一页有多少条数据</param>
        /// <param name="key">要查询的关键字</param>
        /// <param name="statuss">根据多个状态过滤</param>
        /// <returns></returns>
        [HttpGet]
		[CheckLogin(new CheckLoginRoles[] {CheckLoginRoles.business})]
        public async Task<IActionResult> BusinessPagination(int page, int limit, string? key,[FromQuery(Name ="statuss")]List<int>? statuss)
        {
            return Send(await _iOrdersServer.PaginationAsync(page, limit, key, _business.BId, null,statuss));
        }

		/// <summary>
		/// 供应商接单
		/// </summary>
		/// <param name="dto"></param>
		/// <returns></returns>
		[HttpPost]
		[CheckLogin(new CheckLoginRoles[] {CheckLoginRoles.suppliers})]
        public async Task<IActionResult> TakeOrder(RequestIdOrderDto dto)
		{
			return Send(await _iOrdersServer.TakeOrder(dto.id, _supplier.SId));
		}
        /// <summary>
        /// 供应商发货
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [CheckLogin(new CheckLoginRoles[] { CheckLoginRoles.suppliers })]
        public async Task<IActionResult> Shipment(RequestIdOrderDto dto)
		{
			return Send(await _iOrdersServer.Shipment(dto.id,_supplier.SId));
		}
		/// <summary>
		/// 商家收货
		/// </summary>
		/// <param name="dto"></param>
		/// <returns></returns>
		[HttpPost]
        [CheckLogin(new CheckLoginRoles[] { CheckLoginRoles.business })]
		public async Task<IActionResult> AcceptOrder(AcceptOrderDto dto)
		{
			return Send(await _iOrdersServer.AcceptOrder(dto.id,dto.odids,dto.counts,_business.BId));
		}
    }
}
	